﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class MovieDataEditor : UserControl
	{
		private Video videoinfo;

		public MovieDataEditor()
		{
			InitializeComponent();
		}

		public void Initialize(Movie entity)
		{
			videoinfo = entity.VideoInfo;

			if (checkedListBoxGenres.Items.Count == 0)
			{
				GenreBL gbl = new GenreBL();
				checkedListBoxGenres.Items.AddRange(gbl.List().ToArray());
			}

			FillSuggestions(entity);
			FillData();
		}

		private void FillSuggestions(Movie entity)
		{
			MovieBL mbl = new MovieBL();
			if (entity.Suggestions.Count == 0)
				entity.Suggestions = mbl.GetSuggestions(entity);

			comboBoxMovieSuggestions.DataSource = mbl.GetSuggestions(entity);
		}

		private void FillData()
		{
			if (comboBoxMovieSuggestions.Items.Count > 0)
			{
				FillData((Movie)(comboBoxMovieSuggestions.SelectedValue));
			}
			else
			{
				MovieBL mbl = new MovieBL();

				FillData(mbl.Create(videoinfo, false, false));
			}
		}

		public void FillData(Movie entity)
		{
			Tag = entity;
			
			textBoxTitle.Text = entity.Title;
			textBoxYear.Text = entity.Year.ToString();
			textBoxDirector.Text = entity.Director;
			textBoxTagline.Text = entity.TagLine;

			for (int i = 0; i < checkedListBoxGenres.Items.Count; i++)
			{
				checkedListBoxGenres.SetItemChecked(i, false);
				for (int j = 0; j < entity.Genres.Count; j++)
					if (checkedListBoxGenres.Items[i].ToString().Contains(entity.Genres[j].Name))
						checkedListBoxGenres.SetItemChecked(i, true);
			}

			ratingControlRating.SetRating(entity.Rating);

			if (entity.Poster == "")
				entity.Poster = "http://hpneo.com/DoubleFeature/img/poster_default.jpg";

			try
			{
				pictureBoxPoster.Image = Image.FromStream(System.Net.WebRequest.Create(entity.Poster).GetResponse().GetResponseStream());
			}
			catch (Exception e)
			{
				MessageBox.Show("Unable to get poster.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}

			labelLocation.Text = "Location: "+entity.VideoInfo.FileName;

			pictureBoxPoster.Tag = entity.Poster;
		}

		private void comboBoxMovieSuggestions_SelectedIndexChanged(object sender, EventArgs e)
		{
			Cursor = Cursors.WaitCursor;
			if (comboBoxMovieSuggestions.SelectedValue != null)
			{
				Movie selectedmovie = (Movie)(comboBoxMovieSuggestions.SelectedValue);
				selectedmovie.VideoInfo = videoinfo;

				FillData(selectedmovie);

				((TaskDialogAddMovieStep2)Parent).UpdateSelectedMovie((Movie)(Tag));
			}
			Cursor = Cursors.Default;
		}

		private void checkedListBoxGenres_Leave(object sender, EventArgs e)
		{
			Movie entity = ((Movie)Tag);
			entity.Genres.Clear();

			for (int i = 0; i < checkedListBoxGenres.CheckedItems.Count; i++)
				entity.Genres.Add((Genre)(checkedListBoxGenres.CheckedItems[i]));
		}

		private void buttonAddGenre_Click(object sender, EventArgs e)
		{
			if (textBoxAddGenre.Text.Trim() != "")
			{
				GenreBL gbl = new GenreBL();

				Genre g = new Genre();
				g.Name = textBoxAddGenre.Text.Trim();

				if (gbl.Save(g))
				{
					g = gbl.GetLastAdded();
					checkedListBoxGenres.Items.Add(g, true);
				}
			}
		}

		private void pictureBoxPoster_Click(object sender, EventArgs e)
		{
			MessageBox.Show("Changing Poster");
		}

		private void buttonAddAPart_Click(object sender, EventArgs e)
		{
			if (openFileDialogAddAPart.ShowDialog() == DialogResult.OK && openFileDialogAddAPart.FileNames.Length > 0)
			{
				Movie entity = ((Movie)Tag);

				List<string> filenames = new List<string>();

				filenames.Add(entity.VideoInfo.FileName);
				filenames.AddRange(openFileDialogAddAPart.FileNames);

				entity.VideoInfo.FileName = String.Join("|", filenames.ToArray());

				labelLocation.Text = "Location: " + entity.VideoInfo.FileName;
			}
		}
	}
}